home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH6 / SRC / BSPLINE.FRM < prev    next >
Text File  |  1996-04-01  |  11KB  |  416 lines

  1. VERSION 4.00
  2. Begin VB.Form BsplineForm 
  3.    Caption         =   "B-spline"
  4.    ClientHeight    =   5430
  5.    ClientLeft      =   2175
  6.    ClientTop       =   930
  7.    ClientWidth     =   4830
  8.    Height          =   6120
  9.    Left            =   2115
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   362
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   322
  14.    Top             =   300
  15.    Width           =   4950
  16.    Begin VB.CheckBox ShowTCheck 
  17.       Caption         =   "Show t Values"
  18.       Height          =   255
  19.       Left            =   1680
  20.       TabIndex        =   8
  21.       Top             =   300
  22.       Width           =   1755
  23.    End
  24.    Begin VB.TextBox KText 
  25.       Height          =   285
  26.       Left            =   1140
  27.       TabIndex        =   6
  28.       Text            =   "3"
  29.       Top             =   45
  30.       Width           =   375
  31.    End
  32.    Begin VB.CommandButton CmdNew 
  33.       Caption         =   "New"
  34.       Enabled         =   0   'False
  35.       Height          =   375
  36.       Left            =   4320
  37.       TabIndex        =   5
  38.       Top             =   0
  39.       Width           =   495
  40.    End
  41.    Begin VB.CommandButton CmdGo 
  42.       Caption         =   "Go"
  43.       Default         =   -1  'True
  44.       Enabled         =   0   'False
  45.       Height          =   375
  46.       Left            =   3600
  47.       TabIndex        =   4
  48.       Top             =   0
  49.       Width           =   495
  50.    End
  51.    Begin VB.CheckBox ControlCheck 
  52.       Caption         =   "Show Control Points"
  53.       Height          =   255
  54.       Left            =   1680
  55.       TabIndex        =   3
  56.       Top             =   0
  57.       Value           =   1  'Checked
  58.       Width           =   1755
  59.    End
  60.    Begin VB.TextBox DtText 
  61.       Height          =   285
  62.       Left            =   240
  63.       TabIndex        =   2
  64.       Text            =   "0.05"
  65.       Top             =   45
  66.       Width           =   615
  67.    End
  68.    Begin VB.PictureBox Canvas 
  69.       AutoRedraw      =   -1  'True
  70.       Height          =   4815
  71.       Left            =   0
  72.       ScaleHeight     =   317
  73.       ScaleMode       =   3  'Pixel
  74.       ScaleWidth      =   317
  75.       TabIndex        =   0
  76.       Top             =   600
  77.       Width           =   4815
  78.    End
  79.    Begin VB.Label Label1 
  80.       Caption         =   "K"
  81.       Height          =   255
  82.       Index           =   0
  83.       Left            =   960
  84.       TabIndex        =   7
  85.       Top             =   60
  86.       Width           =   255
  87.    End
  88.    Begin VB.Label Label1 
  89.       Caption         =   "dt"
  90.       Height          =   255
  91.       Index           =   1
  92.       Left            =   0
  93.       TabIndex        =   1
  94.       Top             =   60
  95.       Width           =   255
  96.    End
  97.    Begin VB.Menu mnuFile 
  98.       Caption         =   "&File"
  99.       Begin VB.Menu mnuFileExit 
  100.          Caption         =   "E&xit"
  101.       End
  102.    End
  103. End
  104. Attribute VB_Name = "BsplineForm"
  105. Attribute VB_Creatable = False
  106. Attribute VB_Exposed = False
  107. Option Explicit
  108.  
  109. Const PI = 3.14159
  110.  
  111. Const GAP = 3
  112.  
  113. ' The endpoints are points 1 and 4. The control
  114. ' points are points 2 and 3.
  115. Dim MaxPt As Integer
  116. Dim PtX() As Single
  117. Dim PtY() As Single
  118.  
  119. Dim MakingNew As Boolean
  120.  
  121. ' The index of the point being dragged.
  122. Dim Dragging As Integer
  123.  
  124. Dim OldMode As Integer
  125.  
  126. ' Kvalue determines the smoothness of the curve.
  127. Dim Kvalue As Integer
  128.  
  129. ' t runs between 0 and MaxPt - Kvalue + 2.
  130. Dim MaxT As Single
  131.  
  132. ' ************************************************
  133. ' Recursively compute the blending function.
  134. ' ************************************************
  135. Function Blend(i As Integer, k As Integer, t As Single) As Single
  136. Dim numer As Single
  137. Dim denom As Single
  138. Dim value1 As Single
  139. Dim value2 As Single
  140.  
  141.     ' Base case for the recursion.
  142.     If k = 1 Then
  143.         If Knot(i) <= t And t < Knot(i + 1) Then
  144.             Blend = 1
  145.         ElseIf t = MaxT And Knot(i) <= t And t <= Knot(i + 1) Then
  146.             Blend = 1
  147.         Else
  148.             Blend = 0
  149.         End If
  150.         Exit Function
  151.     End If
  152.     
  153.     denom = Knot(i + k - 1) - Knot(i)
  154.     If denom = 0 Then
  155.         value1 = 0
  156.     Else
  157.         numer = (t - Knot(i)) * Blend(i, k - 1, t)
  158.         value1 = numer / denom
  159.     End If
  160.     
  161.     denom = Knot(i + k) - Knot(i + 1)
  162.     If denom = 0 Then
  163.         value2 = 0
  164.     Else
  165.         numer = (Knot(i + k) - t) * Blend(i + 1, k - 1, t)
  166.         value2 = numer / denom
  167.     End If
  168.  
  169.     Blend = value1 + value2
  170. End Function
  171.  
  172. ' ************************************************
  173. ' Draw the curve on the indicated picture box.
  174. ' ************************************************
  175. Sub DrawCurve(pic As PictureBox, start_t As Single, stop_t As Single, dt As Single)
  176. Dim x1 As Single
  177. Dim y1 As Single
  178. Dim t As Single
  179.  
  180.     x1 = X(start_t)
  181.     y1 = Y(start_t)
  182.     pic.Cls
  183.     pic.CurrentX = x1
  184.     pic.CurrentY = y1
  185.     
  186.     t = start_t + dt
  187.     Do While t < stop_t
  188.         x1 = X(t)
  189.         y1 = Y(t)
  190.         pic.Line -(x1, y1)
  191.         t = t + dt
  192.     Loop
  193.     
  194.     x1 = X(stop_t)
  195.     y1 = Y(stop_t)
  196.     pic.Line -(x1, y1)
  197. End Sub
  198.  
  199.  
  200. ' ************************************************
  201. ' Return the ith knot value.
  202. ' ************************************************
  203. Function Knot(i As Integer) As Integer
  204.     If i < Kvalue Then
  205.         Knot = 0
  206.     ElseIf i <= MaxPt Then
  207.         Knot = i - Kvalue + 1
  208.     Else
  209.         Knot = MaxPt - Kvalue + 2
  210.     End If
  211. End Function
  212.  
  213.  
  214. ' ************************************************
  215. ' The parametric function Y(t).
  216. ' ************************************************
  217. Function Y(t As Single) As Single
  218. Dim i As Integer
  219. Dim value As Single
  220.  
  221.     For i = 0 To MaxPt
  222.         value = value + PtY(i) * Blend(i, Kvalue, t)
  223.     Next i
  224.     Y = value
  225. End Function
  226.  
  227. ' ************************************************
  228. ' The parametric function X(t).
  229. ' ************************************************
  230. Function X(t As Single) As Single
  231. Dim i As Integer
  232. Dim value As Single
  233.  
  234.     For i = 0 To MaxPt
  235.         value = value + PtX(i) * Blend(i, Kvalue, t)
  236.     Next i
  237.     X = value
  238. End Function
  239.  
  240. ' ************************************************
  241. ' Use DrawCurve to draw the Bezier curve.
  242. ' ************************************************
  243. Private Sub DrawBspline()
  244. Const DOTTED = 2
  245.  
  246. Dim dt As Single
  247. Dim i As Integer
  248. Dim oldstyle As Integer
  249.  
  250.     If MaxPt < 0 Then Exit Sub
  251.     
  252.     MousePointer = vbHourglass
  253.     
  254.     Kvalue = CInt(KText.Text)
  255.     dt = CSng(DtText.Text)
  256.     MaxT = MaxPt - Kvalue + 2
  257.     DrawCurve Canvas, 0, MaxT, dt
  258.  
  259.     If ControlCheck.value = vbChecked Then
  260.         ' Draw the control points.
  261.         For i = 0 To MaxPt
  262.             Canvas.Line _
  263.                 (PtX(i) - GAP, PtY(i) - GAP)- _
  264.                 Step(2 * GAP, 2 * GAP), , BF
  265.         Next i
  266.         
  267.         ' Connect the control points.
  268.         oldstyle = Canvas.DrawStyle
  269.         Canvas.DrawStyle = DOTTED
  270.         Canvas.CurrentX = PtX(0)
  271.         Canvas.CurrentY = PtY(0)
  272.         For i = 1 To MaxPt
  273.             Canvas.Line -(PtX(i), PtY(i))
  274.         Next i
  275.         Canvas.DrawStyle = oldstyle
  276.     End If
  277.  
  278.     ' Mark the t values if desired.
  279.     If ShowTCheck.value = vbChecked Then
  280.         For dt = 0 To MaxT Step 1#
  281.             Canvas.Line (X(dt), Y(dt) - 5)-Step(0, 10)
  282.             Canvas.Line (X(dt) - 5, Y(dt))-Step(10, 0)
  283.         Next dt
  284.     End If
  285.  
  286.     MousePointer = vbDefault
  287. End Sub
  288.  
  289. ' ************************************************
  290. ' Either collect a new point or select a point and
  291. ' start dragging it.
  292. ' ************************************************
  293. Private Sub Canvas_MouseDown(button As Integer, Shift As Integer, X As Single, Y As Single)
  294. Dim i As Integer
  295.  
  296.     ' If we are selecting points, do so now.
  297.     If MakingNew Then
  298.         MaxPt = MaxPt + 1
  299.         ReDim Preserve PtX(0 To MaxPt)
  300.         ReDim Preserve PtY(0 To MaxPt)
  301.         PtX(MaxPt) = X
  302.         PtY(MaxPt) = Y
  303.         Canvas.Line _
  304.             (X - GAP, Y - GAP)- _
  305.             Step(2 * GAP, 2 * GAP), , BF
  306.         
  307.         If MaxPt >= 3 Then CmdGo.Enabled = True
  308.         
  309.         Exit Sub
  310.     End If
  311.  
  312.     ' Otherwise start dragging a point.
  313.     ' Find a close point.
  314.     For i = 0 To MaxPt
  315.         If Abs(PtX(i) - X) <= GAP And _
  316.            Abs(PtY(i) - Y) <= GAP Then Exit For
  317.     Next i
  318.     If i > MaxPt Then Exit Sub
  319.  
  320.     Dragging = i
  321.     OldMode = Canvas.DrawMode
  322.     Canvas.DrawMode = vbInvert
  323.     PtX(Dragging) = X
  324.     PtY(Dragging) = Y
  325.     Canvas.Line _
  326.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  327.         Step(2 * GAP, 2 * GAP), , BF
  328. End Sub
  329.  
  330.  
  331. ' ************************************************
  332. ' Continue dragging a point.
  333. ' ************************************************
  334. Private Sub Canvas_MouseMove(button As Integer, Shift As Integer, X As Single, Y As Single)
  335.     If Dragging < 0 Then Exit Sub
  336.     
  337.     Canvas.Line _
  338.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  339.         Step(2 * GAP, 2 * GAP), , BF
  340.     
  341.     PtX(Dragging) = X
  342.     PtY(Dragging) = Y
  343.     
  344.     Canvas.Line _
  345.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  346.         Step(2 * GAP, 2 * GAP), , BF
  347. End Sub
  348.  
  349.  
  350. ' ************************************************
  351. ' Finish the drag and redraw the curve.
  352. ' ************************************************
  353. Private Sub Canvas_MouseUp(button As Integer, Shift As Integer, X As Single, Y As Single)
  354.     If Dragging < 0 Then Exit Sub
  355.     
  356.     Canvas.DrawMode = OldMode
  357.     
  358.     PtX(Dragging) = X
  359.     PtY(Dragging) = Y
  360.     Dragging = -1
  361.     
  362.     DrawBspline
  363. End Sub
  364.  
  365.  
  366.  
  367.  
  368. Private Sub CmdGo_Click()
  369.     MakingNew = False
  370.     CmdNew.Enabled = True
  371.     DrawBspline
  372. End Sub
  373.  
  374. ' ************************************************
  375. ' Prepare to get new points.
  376. ' ************************************************
  377. Private Sub CmdNew_Click()
  378.     MaxPt = -1
  379.     CmdGo.Enabled = False
  380.     CmdNew.Enabled = False
  381.     MakingNew = True
  382.     Canvas.Cls
  383. End Sub
  384.  
  385. Private Sub ControlCheck_Click()
  386.     DrawBspline
  387. End Sub
  388.  
  389. Private Sub Form_Load()
  390.     MakingNew = True
  391.     MaxPt = -1
  392.     Dragging = -1
  393. End Sub
  394.  
  395. ' ************************************************
  396. ' Make the canvas as big as possible.
  397. ' ************************************************
  398. Private Sub Form_Resize()
  399.     Canvas.Move 0, Canvas.Top, _
  400.         ScaleWidth, ScaleHeight - Canvas.Top
  401.         
  402.     DrawBspline
  403. End Sub
  404.  
  405.  
  406. Private Sub mnuFileExit_Click()
  407.     Unload Me
  408. End Sub
  409.  
  410.  
  411. Private Sub ShowTCheck_Click()
  412.     DrawBspline
  413. End Sub
  414.  
  415.  
  416.